package com.scau.service.impl;

import com.scau.dao.ClassScheduleDao;
import com.scau.dao.LectureRecordDao;
import com.scau.dao.StudentDao;
import com.scau.entity.ClassSchedule;
import com.scau.entity.LectureRecord;
import com.scau.entity.Student;
import com.scau.service.LectureRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LectureRecordServiceImpl implements LectureRecordService {

    @Autowired
    private ClassScheduleDao classScheduleDao;
    @Autowired
    private LectureRecordDao lectureRecordDao;
    @Autowired
    private StudentDao studentDao;
    @Override
    public void askForLeave(LectureRecord lectureRecord) {

        ClassSchedule classSchedule = classScheduleDao.findOne(lectureRecord.getClassId());

        LectureRecord oldRecord = lectureRecordDao.findByClassIdAndStudentId(lectureRecord.getClassId(), lectureRecord.getStudentId());

        if (oldRecord != null) {
            lectureRecord = oldRecord;
        }
        lectureRecord.setStartTime(classSchedule.getStartTime());
        lectureRecord.setEndTime(classSchedule.getEndTime());
        lectureRecord.setLeft(true);

        Student student = studentDao.findOne(lectureRecord.getStudentId());
        student.setRemainingClass(student.getRemainingClass() + 1);
        studentDao.save(student);

        lectureRecordDao.save(lectureRecord);
    }
}
